#!/bin/bash

# 目标工作目录
WORK_DIR="/root/workspace"

# 改变到目标工作目录
cd "$WORK_DIR" || { echo "Failed to change directory to $WORK_DIR"; exit 1; }

# 设置环境变量
export DJANGO_SETTINGS_MODULE=HopeIESystem.settings
export PYTHONPATH=$PYTHONPATH:$WORK_DIR

# 检查screen命令 是否已安装
if ! command -v screen &> /dev/null
then
    echo "screen could not be found. Please install it and try again."
    exit 1
fi

# 在当前目录下安装项目依赖
/root/miniconda3/envs/django/bin/python -m pip install -r requirements.txt



# 启动Celery worker的新Screen会话
screen -S celery_worker -dm bash -c "cd $WORK_DIR; exec /root/miniconda3/envs/django/bin/celery -A HopeIESystem worker --loglevel=info --logfile=celery.log"

# 启动Celery beat的新Screen会话
screen -S celery_beat -dm bash -c "cd $WORK_DIR; exec /root/miniconda3/envs/django/bin/celery -A HopeIESystem beat --loglevel=info --logfile=celery_beat.log"

# 启动Django服务器（注意：这里直接在当前目录执行，没有特别指定后台运行，因为在screen会话中管理更灵活）
/root/miniconda3/envs/django/bin/python manage.py runserver 0.0.0.0:8000

echo "All services have been started in separate Screen sessions within $WORK_DIR.
- Django Server: screen -r celery_worker
- Celery Worker: screen -r celery_beat"

# 注意：上面的Django服务器启动命令已经修改为后台运行，且Celery worker和beat在各自的Screen会话中启动，
# 确保了所有操作都在指定目录下执行。